# Attention: This can only run under WINDOWS
from win32com.client import constants,gencache
import os
def createpdf(wordPath,pdfPath):
    word=gencache.EnsureDispatch('Word.Application')
    doc=word.Documents.Open(wordPath,ReadOnly=1)
    doc.ExportAsFixedFormat(pdfPath,constants.wdExportFormatPDF)
    word.Quit()
# 单个文件的转换
# createpdf('D:\pythonProject\info.docx','D:\pythonProject\info.pdf')
# 多个文件的转换
wordfiles=[]
for file in os.listdir('.'):
    if file.endswith(('.doc','.docx')):
        wordfiles.append(file)
for file in wordfiles:
    filepath=os.path.abspath(file)
    index=filepath.rindex('.')
    pdfpath=filepath[:index]+'.pdf'
    createpdf(filepath,pdfpath)
